/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx.config;

import org.apache.ibatis.annotations.Mapper;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.io.IOException;

/**
 * <p>Project: mybatis2021 - SpringConfig
 * <p>Powered by webrx On 2021-07-20 11:23:25
 * <p>Created by IntelliJ IDEA
 * 此程序相当于spring applicationContext.xml beans.xml配置文件
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 15
 */
@Configuration
@ComponentScan(basePackages = {"cn.webrx.entity"})
@Import(DruidConfig.class)
@MapperScan(basePackages = "cn.webrx.mapper",sqlSessionFactoryRef = "sf")
public class SpringConfig {
    @Bean("sf")
    public SqlSessionFactoryBean sqlSessionFactory(DataSource ds) throws IOException {
        var sf = new SqlSessionFactoryBean();
        sf.setTypeAliasesPackage("cn.webrx.entity");
        sf.setDataSource(ds);
        sf.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mappers/*.xml"));
        return sf;
    }

    /*
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        var msc = new MapperScannerConfigurer();
        msc.setBasePackage("cn.webrx.mapper");//默认使用bean name StudentMapper 名称就是studetMapper
        msc.setSqlSessionFactoryBeanName("sf");
        return msc;
    }

     */


}
